package org.elip.teststatistics.server.xml;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;


public class XMLParser {

	private final static String testsuitesPath = "org/elip/teststatistics/server/xml/test-suites.xml";

	public static String[] parseProducts() throws XMLStreamException, IOException {

		List<String> list = new ArrayList<String>();		
		InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(testsuitesPath);
		XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(is);

		parser.nextTag();
		while (true) {
			if ((parser.getEventType() == XMLStreamConstants.END_ELEMENT) 
					&& parser.getName().toString().equals("products")) break;
			if (parser.getEventType() == XMLStreamConstants.START_ELEMENT) {
				if (parser.getName().toString().equals("product")) {
					list.add(parser.getAttributeValue(null, "name"));
				}
			}
			parser.nextTag();
		}
		String[] result = new String[list.size()];
		is.close();
		parser.close();
		return list.toArray(result);
	}

	public static String[] parseSuites(String product) throws XMLStreamException, FactoryConfigurationError, IOException {

		List<String> list = new ArrayList<String>();		
		InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(testsuitesPath);
		XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(is);

		parser.nextTag();
		while (true) {
			if ((parser.getEventType() == XMLStreamConstants.END_ELEMENT) 
					&& parser.getName().toString().equals("products")) break;
			if (parser.getEventType() == XMLStreamConstants.START_ELEMENT) {
				if (parser.getName().toString().equals("product")) {
					if (parser.getAttributeValue(null, "name").equals(product)) {
						parser.nextTag();
						while (true) {
							if ((parser.getEventType() == XMLStreamConstants.END_ELEMENT) 
									&& parser.getName().toString().equals("product")) break;
							if (parser.getEventType() == XMLStreamConstants.START_ELEMENT) {
								if (parser.getName().toString().equals("suite")) {
									list.add(parser.getAttributeValue(null, "name"));
								}
							}
							parser.nextTag();
						}
					}
				}
			}
			parser.nextTag();
		}
		String[] result = new String[list.size()];
		is.close();
		parser.close();
		return list.toArray(result);

	}

	public static String[] parsePackages(String product, String suite) throws XMLStreamException, FactoryConfigurationError, IOException {

		List<String> list = new ArrayList<String>();		
		InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(testsuitesPath);
		XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(is);

		parser.nextTag();
		while (true) {
			if ((parser.getEventType() == XMLStreamConstants.END_ELEMENT) 
					&& parser.getName().toString().equals("products")) break;
			if (parser.getEventType() == XMLStreamConstants.START_ELEMENT) {
				if (parser.getName().toString().equals("product")) {
					if (parser.getAttributeValue(null, "name").equals(product)) {

						while (true) {
							if ((parser.getEventType() == XMLStreamConstants.END_ELEMENT) 
									&& parser.getName().toString().equals("product")) break;
							if (parser.getEventType() == XMLStreamConstants.START_ELEMENT) {
								if (parser.getName().toString().equals("suite")) {
									if (parser.getAttributeValue(null, "name").equals(suite)) {

										while (true) {
											if ((parser.getEventType() == XMLStreamConstants.END_ELEMENT) 
													&& parser.getName().toString().equals("packages")) break;
											if (parser.getEventType() == XMLStreamConstants.START_ELEMENT) {
												if (parser.getName().toString().equals("package")) {
													list.add(parser.getAttributeValue(null, "path"));
												}
											}
											parser.nextTag();
										}
									}
								}
							}
							parser.nextTag();
						}
					}
				}
			}
			parser.nextTag();
		}
		String[] result = new String[list.size()];
		is.close();
		parser.close();
		return list.toArray(result);
	}
}
